package cn.share.life.module.images.utils;

import cn.hutool.core.io.FileUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;

public class Move500Utils {

    /**
     * 图片文件名
     */
    private static final String IMAGES_FILE_NAME = "image_";


    public static void main(String[] args) throws IOException {
//        HtmlFidAndPathEnums[] values = HtmlFidAndPathEnums.values();
//        for (HtmlFidAndPathEnums value : values) {
//            totalFileCount(value.getBasePath());
//        }

        totalFileCount("E:/image/siwameitui");

//        HtmlFidAndPathEnums2 fidEnum = HtmlFidAndPathEnums2.FidAndPath_14;
//        String path = fidEnum.getBasePath() + fidEnum.getImagePath();
//        moveFileTo5000(path);
//        moveFileTo5000(HtmlFidAndPathEnums.FidAndPath_21);
    }

    /**
     * 移动文件到5000
     */
    private static void moveFileTo5000(String path) throws IOException {
        List<File> imageFileList = ImageFileUtils.getImageFileList(path);
        System.out.println("文件数量: " + imageFileList.size());

        List<List<File>> partition = Lists.partition(imageFileList, 5000);
        for (int i = 0; i < partition.size(); i++) {
            List<File> files = partition.get(i);
            String targetDirectoryPath = path + "/" + IMAGES_FILE_NAME + (i + 1);
            System.out.println("文件路径: " + targetDirectoryPath);
            Path targetDirectory = Paths.get(targetDirectoryPath);
            // 检查源文件是否存在
            if (!Files.exists(targetDirectory)) {
                Files.createDirectory(targetDirectory);
            } else if (FileUtil.listFileNames(targetDirectoryPath).size() >= 5000) {
                continue;
            }

            for (File file : files) {
                // 构建目标文件路径
                Path targetFilePath = targetDirectory.resolve(file.getName());
                Files.move(file.toPath(), targetFilePath, StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }

    /**
     * 同条数
     */
    private static void totalFileCount(String path) {
        Map<String, String> imageFileNameMap = ImageFileUtils.getImageFileNameMap(path);
        System.out.println(path + " 文件数量: " + imageFileNameMap.size());
        List<String> allFileLinks = ImageFileUtils.getAllFileLinks(path);
        System.out.println(path + " 文件链接数量: " + allFileLinks.size());

        if (imageFileNameMap.isEmpty()) {
            System.out.println(path + " 文件名为空");
            return;
        }

        int count = 0;
        for (String fileName : allFileLinks) {
            String filePath = fileName.substring(fileName.lastIndexOf("/") + 1);
            if (StringUtils.isBlank(imageFileNameMap.getOrDefault(filePath, ""))) {
                count++;
            }
        }
        System.out.println(path + " 文件不存在的数量: " + count);
    }


}
